//免登录接口
let noLoginUrl = [
	'/addons/career/api.v1.common/init',
	'/addons/career/api.v1.login/login',
	'/addons/career/api.v1.login/mobilelogin',
	'/addons/career/api.v1.login/register',
	'/addons/career/api.v1.login/resetpwd',
	'/addons/career/api.v1.login/wxLogin',
	'/addons/career/api.v1.login/wechatMobileLogin',
	'/addons/career/api.v1.login/appLogin',
	'/addons/career/api.v1.login/getWechatOpenid',
	'/addons/career/api.v1.sms/send',
	'/addons/career/api.v1.archives/policy',
	'/addons/career/api.v1.archives/policyDetail',
	'/addons/career/api.v1.archives/trainee',
	'/addons/career/api.v1.archives/traineeDetail',
	'/addons/career/api.v1.course/index',
	'/addons/career/api.v1.common/college',
	'/addons/career/api.v1.common/major',
	'/addons/career/api.v1.course/detail',
	'/addons/career/api.v1.course/chapter',
	'/addons/career/api.v1.comment/index',
	'/addons/career/api.v1.classes/index',
	'/addons/career/api.v1.classes/detail',
	'/addons/career/api.v1.course_video/watch'
];

//设置session_id
const getSessionId = function(vm) {
	let session = vm.$util.getDb('session');
	if (!session) {
		let guid = vm.$u.guid();
		vm.$util.setDb('session', guid);
		return guid;
	}
	return session;
}
// 这里的vm，就是我们在vue文件里面的this，所以我们能在这里获取vuex的变量，比如存放在里面的token
// 同时，我们也可以在此使用getApp().globalData，如果你把token放在getApp().globalData的话，也是可以使用的
const install = (Vue, vm) => {
	//let url = 'https://xy.kmmmkj.cn'
	let url = 'http://ob.kmjsxy.com'
	if(process.env.NODE_ENV === 'development'){
		url = 'http://ob.kmjsxy.com'
	}
	// #ifdef H5
	if (typeof window.fastUrl !== 'undefined') {
		url = window.fastUrl;
	}
	// #endif
	uni.$u.http.setConfig((config) => {
	    /* config 为默认全局配置*/
	    config.baseURL = url; /* 根域名 */
		config.header={'content-type': 'application/json'};
		config.originalData = true;
	    return config
	})
	// Vue.prototype.$u.http.setConfig({
	// 	baseUrl: url,
	// 	header: {
	// 		'content-type': 'application/json'
	// 	},
	// 	originalData: true
	// });
	// 请求拦截，配置Token等参数
	uni.$u.http.interceptors.request.use((config) => {
		//在需要登录的接口，请求前判断token 是否存在,不存在则到登录
		let url = config.url.split('?').shift();
		
		if (!noLoginUrl.includes(url) && !vm.vuex_token) {
			uni.$u.route({url:'/pages/login/mobilelogin'});
			return false;
		}
		config.header.token = vm.vuex_token;
		//设置session_id
		config.header.sid = getSessionId(vm);
		config.header.uid = vm.vuex_user.id || 0;
		const res = uni.getSystemInfoSync();
		config.header.platform = res.platform || '';
		config.header.model = res.model || '';
		config.header.brand = res.brand || '';
		config.header['x-requested-with'] = 'xmlhttprequest';
		if (config.method == 'POST') {
			config.data['__token__'] = vm.vuex__token__;
		}
		return config;
	}, config => { // 可使用async await 做异步操作
		return Promise.reject(config)
	})
	// 响应拦截，判断状态码是否通过
	uni.$u.http.interceptors.response.use((res) => {
		//返回__token__,设置
		if (res.header && res.header.__token__) {
			vm.$u.vuex('vuex__token__', res.header.__token__);
		}
		let result = res.data || {};
		if (result.data && result.data.__token__) {
			vm.$u.vuex('vuex__token__', result.data.__token__);
		}
		switch (result.code) {
			case 1:
			case 0:
				return result;
				break;
			case 401:
				//需要登录的接口，当token 过期时，到登录页面
				vm.$u.vuex('vuex_token', '');
				vm.$u.route('/pages/login/mobilelogin');
				return result;
				break;
			case 403: //没有权限访问
				uni.showToast({
					icon: "none",
					title: result.msg
				})
				return result;
				break;
			default:
				if (res.statusCode == 200) {
					return res.data;
				} else {
					console.error(res)
					uni.$u.toast('网络请求错误！');
					return false;
				}
		}

	},(response) => { 
		// 对响应错误做点什么 （statusCode !== 200）
		return Promise.reject(response)
	});
}

export default {
	install
}
